<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>告警项详情</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--列表显示-->
<table id="userTable" lay-filter="userFilter"></table>
<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!--用户更新form-->
<form id="userUpdateForm" class="layui-form" lay-filter="userUpdateFilter" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input type="hidden" name="userId">
    <input name="userId" hidden="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">阈值上限</label>
        <div class="layui-input-block">
            <input type="text" name="max" placeholder="请输入数值"  required lay-verify="required"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div> <div class="layui-form-item">
        <label class="layui-form-label">阈值下限</label>
        <div class="layui-input-block">
            <input type="text" name="min" placeholder="请输入数值"  required lay-verify="required"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateUserFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>



<!--js 代码-->
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table','layer','jquery','form','upload'], function() {
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        var form = layui.form;
        var upload = layui.upload;

        //用户列表js
        let userTable = table.render({
            elem: '#userTable'
            , toolbar: '#toolbarHead'
            , url: '/monitorEvent/queryAll' //数据接口
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'eventId', title: '编号', width: 80, sort: true}
                    , {field: 'unitId', title: '监控项id', width: 100}
                    , {field: 'monitorValue', title: '监控项的值', width: 120}
                    , {field: 'createTime', title: '创建时间', width: 160},
                    {fixed: 'right', align: 'center', width: 160, toolbar: '#barRowEdit'}
                ]
            ]
        });

        let $deptSelect = $("select[name=deptId]");
        let $roleCheckBox = $("div[name=roles]");
        let options = '';
        let inputs = '';
        //行编辑工具栏监听事件
        table.on('tool(userFilter)', function (obj) {
            //获取当前行数据
            var data = obj.data;
            //==不匹配类型  1=='1' true      ==== 匹配类型  1==='1' false
            switch (obj.event) {
                case 'del':
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/monitorEvent/deleteById",
                            type: "DELETE",
                            data: {userId: data.userId},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    userTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'edit':
                    //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                    form.val("userUpdateFilter", data);
                    layer.open({
                        type: 1,
                        area: ['600px', '400px'],
                        content: $('#userUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            }

            //更新监听事件
            form.on('submit(userUpdateFilter)', function (data) {
                layer.msg(JSON.stringify(data.field));
                $.ajax({
                    url: "/monitorUnit/update",
                    type: "PUT",
                    data: data.field,
                    dataType: "json",
                    success: function (resp) {
                        if (resp.code == 0) {
                            layer.msg(resp.msg);
                            //关闭弹框
                            layer.closeAll('page');
                            //重新加载数据
                            userTable.reload();
                        }
                    },
                    error: function (e) {
                        layer.msg("请求失败");
                    }
                });
                return false;
            });

        });
    });
</script>
</body>
</html>